data<-read.csv("figure1_2.csv",header=T,as.is=T)

#######################
# Generate demographics
#######################

df <- read.csv(text="col1,col2")

#age
df<-rbind(df,cbind("Age",round(mean(data$Q4+18),digits=2)))
#male
df<-rbind(df,cbind("Male",round(prop.table(table(data$Q2))[[1]]*100,digits=2)))
#>college
df<-rbind(df,cbind(">College",round(prop.table(table(data$Q6>=5))[[2]]*100,digits=2)))
#white
df<-rbind(df,cbind("White",round(prop.table(table(data$Q3==1))[[2]]*100,digits=2)))
#ideology
df<-rbind(df,cbind("Ideology",round(mean(data$Q7),digits=2)))
names(df)<-c("Variable"," ")
print(xtable(df),include.rownames=FALSE)

data$defright <- car::recode(data$definition, "0=0;1=1;2=1;else=0")
data$Q41 <- car::recode(data$Q41, "1:2=0;3=1;4:5=3;else=NA")
##################
# Figure 2a
##################


d<-table(data$defright)/nrow(data)
names(d)<-c("Correct","Incorrect")
d<-as.data.frame(d)
t(d)
d$t<-"temp"
plot<-ggplot(d, aes(x=t,y=Freq, fill=Var1)) +
	scale_y_continuous(labels = percent_format(),expand = c(0, 0)) +
	scale_x_discrete(labels="") +
	geom_bar(stat='identity') +
	coord_flip() +
	theme_bw() +
	xlab('') +
	ylab('A) Defintion of Bipartisanship') +
	scale_fill_manual(values=c("#BD0B0B", "#10A108")) +
	theme(legend.position='none') +
	annotate("text", x = 1, y = .5, label = "67% Incorrect",color="white")+
	annotate("text", x = 1, y = .15, label = "33% Correct",color="white")
plot
ggsave(filename="f2a.eps", plot=plot,width=5.5,height=1)

##################
# Figure 2b
##################

d<-prop.table(table(data$Q41))

names(d)<-c("Bad","Neutral","Good")
d<-as.data.frame(d)
t(d)
d$t<-"temp"
plot<-ggplot(d, aes(x=t,y=Freq, fill=Var1)) +
	scale_y_continuous(labels = percent_format(),expand = c(0, 0)) +
	scale_x_discrete(labels="") +
	geom_bar(stat='identity') +
	coord_flip() +
	theme_bw() +
	xlab('') +
	ylab('B) Valence of Bipartisanship') +
	scale_fill_manual(values=c("#BD0B0B", "#5D5D5D","#10A108")) +
	theme(legend.position='none') +
	annotate("text", x = 1, y = .92, label = "16% Bad",color="white")+
	annotate("text", x = 1, y = .6, label = "39% Neutral",color="white") +
	annotate("text", x = 1, y = .15, label = "45% Good",color="white")
plot
ggsave(filename="f2b.eps", plot=plot,width=5.5,height=1)


##################
# Figure 1a
##################

prop.table(table(data$Q42,data$party),2)
data<-data[data$Q8<=3,]
data$party <-factor(data$Q8, labels=c("Democrats","Republicans","Independents"))
df<-as.data.frame(t(prop.table(table(data$Q42,data$party),2)))
names(df)<-c("party","Response","Freq")
df$Response <-factor(df$Response, labels=c("Yes","No","Don't know"))
df$Response <-factor(df$Response,levels(df$Response)[c(3,2,1)])
plot<-ggplot(df, aes(x=party,y=Freq, fill=Response)) +
	scale_y_continuous(labels = percent_format(),expand = c(0, 0)) +
	scale_x_discrete() +
	geom_bar(stat='identity') +
	coord_flip() +
	theme_bw() +
	ylab('A) Assessment of Three Republicans Joining Democrats') +
	xlab('') +
	scale_fill_manual(values=c("#5D5D5D","#BD0B0B","#10A108")) +
	annotate("text", x = c(1:3), y = .1, label = c("57%","55%","50%"),color="white")+
	annotate("text", x = c(1:3), y = .65, label = c("20%","25%","26%"),color="white")+
	annotate("text", x = c(1:3), y = .9, label = c("23%","20%","24%"),color="white")
plot

ggsave(filename="f1a.eps", plot=plot,width=5.5,height=1.5)


##################
# Figure 1b
##################

prop.table(table(data$Q43,data$party),2)
data<-data[data$Q8<=3,]
data$party <-factor(data$Q8, labels=c("Democrats","Republicans","Independents"))
df<-as.data.frame(t(prop.table(table(data$Q43,data$party),2)))
names(df)<-c("party","Response","Freq")
df$Response <-factor(df$Response, labels=c("Yes","No","Don't know"))
df$Response <-factor(df$Response,levels(df$Response)[c(3,2,1)])

plot<-ggplot(df, aes(x=party,y=Freq, fill=Response)) +
	scale_y_continuous(labels = percent_format(),expand = c(0, 0)) +
	scale_x_discrete() +
	geom_bar(stat='identity') +
	coord_flip() +
	theme_bw() +
	ylab('B) Assessment of Three Democrats Joining Republicans') +
	xlab('') +
	scale_fill_manual(values=c("#5D5D5D","#BD0B0B","#10A108")) +
	annotate("text", x = c(1:3), y = .1, label = c("58%","57%","45%"),color="white")+
	annotate("text", x = c(1:3), y = c(.7,.7,.6), label = c("21%","33%","23%"),color="white")+
	annotate("text", x = c(1:3), y = .9, label = c("21%","10%","32%"),color="white")
plot

ggsave(filename="f1b.eps", plot=plot,width=5.5,height=1.5)

